package com.sskj.lib.model.repository;


import android.arch.lifecycle.LiveData;
import android.text.TextUtils;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.sskj.common.base.App;
import com.sskj.common.base.HttpData;
import com.sskj.common.util.SPUtil;
import com.sskj.lib.BaseHttpConfig;
import com.sskj.lib.Constans;
import com.sskj.lib.SPConfig;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.bean.UserInfo;
import com.sskj.lib.http.JsonCallBack;
import com.sskj.lib.model.room.UserDao;
import com.tencent.bugly.crashreport.biz.UserInfoBean;

import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import io.reactivex.schedulers.Schedulers;

import static com.sskj.lib.BaseHttpConfig.BASE_URL;

/**
 * Created by lv on 18-5-7.
 */
//@Singleton
public class UserRepository {


    public UserDao userDao;

    @Inject
    public UserRepository(@Named("UserDao") UserDao userDao) {
        this.userDao = userDao;
    }

    public LiveData<List<UserData>> getAllUser() {
        return userDao.getAll();
    }

    public void updateUser() {
        if (TextUtils.isEmpty(SPUtil.get(SPConfig.ACCOUNT, ""))) {
            return;
        }
        OkGo.<HttpData<UserInfo>>get(BASE_URL + "/Home/Users/userInfo")
                .execute(new JsonCallBack<HttpData<UserInfo>>() {
                    @Override
                    public void onHTTPSuccess(HttpData<UserInfo> data) {
                        UserData userData = new UserData();
                        userData.setId(1);
                        userData.setMobile(data.getData().getMobile());
                        userData.setAccount(data.getData().getAccount());
                        userData.setRealname(data.getData().getRealname());
                        userData.setNickname(data.getData().getNickname());
                        userData.setIdcard(data.getData().getIdcard());
                        userData.setIs_set_tpwd(data.getData().getIs_set_tpwd());
                        userData.setUpic(data.getData().getUpic());
                        userData.setBank_username(data.getData().getBank_username());
                        userData.setBank_name(data.getData().getBank_name());
                        userData.setBank_sub_name(data.getData().getBank_sub_name());
                        userData.setBank_nums(data.getData().getBank_nums());
                        userData.setAlipay_username(data.getData().getAlipay_username());
                        userData.setAlipay_nums(data.getData().getAlipay_nums());
                        userData.setAlipay_img(data.getData().getAlipay_img());
                        userData.setUserid(data.getData().getAsset().getUserid());
                        userData.setPid(data.getData().getAsset().getPid());
                        userData.setMark(data.getData().getAsset().getMark());
                        userData.setUsable(data.getData().getAsset().getUsable());
                        userData.setFrost(data.getData().getAsset().getFrost());
                        userData.setCzprice(data.getData().getAsset().getCzprice());
                        userData.setTxprice(data.getData().getAsset().getTxprice());
                        userData.setLock_num(data.getData().getAsset().getLock_num());
                        userData.setIs_ti(data.getData().getAsset().getIs_ti());
                        insert(userData);
                    }

                    @Override
                    public void onError(Response<HttpData<UserInfo>> response) {
                        super.onError(response);
                        System.out.println("1");
                    }
                });
    }


    public void insert(UserData user) {
        Schedulers.newThread().scheduleDirect(() -> userDao.insert(user));
    }

    public void clear() {
        Schedulers.newThread().scheduleDirect(() -> userDao.deleteAll());
    }

    public void update(UserData user) {
        Schedulers.newThread().scheduleDirect(() -> userDao.update(user));
    }

    public void update() {
        updateUser();
    }
}
